package com.aihwcm.entity;

import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import static org.hibernate.criterion.Example.create;

import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import com.aihwcm.action.LoginAction;

/**
 * A data access object (DAO) providing persistence and search support for
 * Aihusers entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.aihwcm.entity.Aihusers
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class AihusersDAO {
	private static Logger logger = LogManager.getLogger(AihusersDAO.class);

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(Aihusers transientInstance) {
		logger.debug("saving Aihusers instance");
		try {
			getCurrentSession().saveOrUpdate(transientInstance);
			logger.debug("save successful");
		} catch (RuntimeException re) {
			logger.error("save failed", re);
			throw re;
		}
	}

	public void delete(Aihusers persistentInstance) {
		logger.debug("deleting Aihusers instance");
		try {
			getCurrentSession().delete(persistentInstance);
			logger.debug("delete successful");
		} catch (RuntimeException re) {
			logger.error("delete failed", re);
			throw re;
		}
	}

	public Aihusers findById(java.lang.Integer id) {
		logger.debug("getting Aihusers instance with id: " + id);
		try {
			Aihusers instance = (Aihusers) getCurrentSession().get(
					"com.aihwcm.entity.Aihusers", id);
			return instance;
		} catch (RuntimeException re) {
			logger.error("get failed", re);
			throw re;
		}
	}

	public List<Aihusers> findByExample(Aihusers instance) {
		logger.debug("finding Aihusers instance by example");
		try {
			List<Aihusers> results = (List<Aihusers>) getCurrentSession()
					.createCriteria("com.aihwcm.entity.Aihusers")
					.add(create(instance)).list();
			logger.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			logger.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		logger.info("finding Aihusers instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Aihusers as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		logger.debug("finding all Aihusers instances");
		try {
			String queryString = "from Aihusers";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			logger.error("find all failed", re);
			throw re;
		}
	}

	public Aihusers merge(Aihusers detachedInstance) {
		logger.debug("merging Aihusers instance");
		try {
			Aihusers result = (Aihusers) getCurrentSession().merge(
					detachedInstance);
			logger.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			logger.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Aihusers instance) {
		logger.debug("attaching dirty Aihusers instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			logger.debug("attach successful");
		} catch (RuntimeException re) {
			logger.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Aihusers instance) {
		logger.debug("attaching clean Aihusers instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			logger.debug("attach successful");
		} catch (RuntimeException re) {
			logger.error("attach failed", re);
			throw re;
		}
	}

	public static AihusersDAO getFromApplicationContext(ApplicationContext ctx) {
		return (AihusersDAO) ctx.getBean("AihusersDAO");
	}

	public int count() {

		try {
			String queryString = "select count(*) from Aihusers";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return ((Long) queryObject.iterate().next()).intValue();

		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Aihusers> listpage(int offset, int limit) {

		try {
			String queryString = "from Aihusers as model order by model.userid desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setFirstResult((offset - 1) * limit);
			queryObject.setMaxResults(limit);

			return queryObject.list();

		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	public int count(String search) {

		try {
			String queryString = "select count(*) from Aihusers as model where model.username LIKE :search";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("search", search);
			return ((Long) queryObject.iterate().next()).intValue();

		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Aihusers> listpage(int offset, int limit, String search) {

		try {
			String queryString = "from Aihusers as model where model.username LIKE :search order by model.userid desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("search", search);
			queryObject.setFirstResult((offset - 1) * limit);
			queryObject.setMaxResults(limit);

			return queryObject.list();

		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

}